package com.zucai365.football.news;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.zucai365.football.utils.ZuCaiUtils;

public class NewsSina extends AbstractNews{
	
	public NewsSina( String lianSai,String[] urls ){
		super( lianSai,urls );
	}
	
	@Override
	public List<String> parse() {
		for( int i=0;i<getUrls().length;i++ ){
			parseJson( getUrls()[i] );
			try {
				Thread.sleep( 2000 );
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	private List<String> parseJson( String url ){
		String html = null;
		try {
			html = ZuCaiUtils.getUrlContent( url , "utf-8" ).trim();
		} catch (Exception e) {
			e.printStackTrace();
		}
		html =  html.substring( "var jsonData = ".length(),html.length() );
		html = html.substring( 0, html.length()-1 );
		JSONArray jsonArray = JSONArray.fromObject( "["+html+"]" );
		
		JSONObject obj = (JSONObject)jsonArray.get(0);		
		JSONArray newsArray = obj.getJSONArray( "list" );
		
		for( int i=0;i<newsArray.size();i++ ){
			JSONObject news = (JSONObject) newsArray.get(i);			
			String title = news.getString( "title" );
			String href = news.getString( "url" );			
			System.out.println( href + "===" + title );
		}
		return null;
	}
	
	public static void main(String[] args) {
		String[] urls = new String[3];
		urls[0] = "http://roll.sports.sina.com.cn/api/news_list.php?tag=2&cat_1=premierleague&cat_2=1&&k=&show_num=60&page=1&r="+Math.random();
		urls[1] = "http://roll.sports.sina.com.cn/api/news_list.php?tag=2&cat_1=premierleague&cat_2=1&&k=&show_num=60&page=2&r="+Math.random();
		urls[2] = "http://roll.sports.sina.com.cn/api/news_list.php?tag=2&cat_1=premierleague&cat_2=1&&k=&show_num=60&page=3&r="+Math.random();
		AbstractNews news = new NewsSina( "yingchao",urls );
		news.parseUrl();
	}
}